<?php
/**
 *房源申请及下单.
 */

namespace App\Controller\Index;

use Common\Controller\App\CommonController;
use Common\Model\AdminModel;
use Common\Model\AppointmentModel;
use Common\Model\HousingModel;
use Common\Model\HousingSkuModel;
use Common\Model\HousekeeperCommentModel;
use Common\Model\MemberCollectionModel;
use Common\Model\OrderModel;

class OrderController extends CommonController
{
    /**
     * 房源预约.
     */
    public function appointment()
    {
        $skuid = I('param.skuid');
        $id = I('param.id');
        if (empty($id) || !is_numeric($id) || empty($skuid) || !is_numeric($skuid)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '编号错误']);
        }
        $model = new HousingModel();
        $info = $model->alias('a')->join('left join neighbourhoods as b on a.neighbourhood=b.id')->field('a.id,a.pic,room_number,housekeeper_id')->where("a.id=$id")->find();
        if ($info) {
            $skumodel = new HousingSkuModel();
            $year_pay = $skumodel->where("pid={$info['id']} and id=$skuid")->field('year_pay')->find();
            $info['money'] = $year_pay['year_pay'];
        } else {
            $info = [];
        }
        $model = new MemberCollectionModel();
        $collertion_res = $model->where("housing_id={$info['id']}")->find();
        if($collertion_res){
            $info['is_collertion'] = 1;
        }else{
            $info['is_collertion'] = 0;
        }
        $adminmodel = new AdminModel();
        $admininfo = $adminmodel->where("id={$info['housekeeper_id']}")->field('username,intro,logo,mobile')->find();
        $info['housekeeper_username'] = $admininfo['username'];
        $info['housekeeper_intro'] = $admininfo['intro'];
        $info['housekeeper_logo'] = $admininfo['logo'];
        $info['housekeeper_mobile'] = $admininfo['mobile'];
        $hmodel = new HousekeeperCommentModel();
        $star = $hmodel->where("housekeeper_id={$info['housekeeper_id']}")->order('create_time desc')->getField('star');
        $info['housekeeper_star'] = $star ? $star : 5;
        $this->ajaxReturn(['code' => 200, 'info' => $info]);
    }

    /**
     * 房源预约提交.
     */
    public function appointment_submit()
    {
        $member_id = session('member_id');
        $name = I('param.name'); //用户名称
        $mobile = I('param.mobile'); //用户名称
        $appointment_time = I('param.appointment_time'); //预约时间
        $housing_id = I('param.housing_id'); //房源id
        $skuid = I('param.skuid'); //房源id
        $describe = I('param.describe'); //留言
        if (empty($name)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '请输入姓名']);
        }
        if (empty($mobile)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '请输入手机号']);
        }
        if (!preg_match("/^1[34578]\d{9}$/", $mobile)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '请输入正确的手机号码']);
        }
        if (empty($appointment_time)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '请选择预约时间']);
        }
        if (empty($housing_id)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '房间编号错误']);
        }
        if (empty($skuid)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '房间编号错误']);
        }
        $data['name'] = $name;
        $data['mobile'] = $mobile;
        $data['appointment_time'] = $appointment_time;
        $data['housing_id'] = $housing_id;
        $data['skuid'] = $skuid;
        $data['describe'] = $describe;
        $data['member_id'] = $member_id;
        $data['create_time'] = date('Y-m-d H:i:s', time());
        $model = new AppointmentModel();
        $res = $model->add($data);
        if ($res) {
            $this->ajaxReturn(['code' => 200, 'msg' => '预约成功']);
        } else {
            $this->ajaxReturn(['code' => 300, 'msg' => '预约失败']);
        }
    }

    /**
     * 获取立即入住页面参数.
     */
    public function get_info()
    {
        $skuid = I('param.skuid');
        $id = I('param.id');
        if (empty($id) || !is_numeric($id) || empty($skuid) || !is_numeric($skuid)) {
            $this->ajaxReturn(['code' => 300, 'msg' => '编号错误']);
        }
        $model = new HousingSkuModel();
        $info = $model->alias('a')->join("housing as b on a.pid=b.id")->where("a.id=$skuid")->field('a.year_pay,a.month_pay,a.season_pay,a.pid,a.deposit_money,a.id,b.room_number')->find();
        $info['total_money'] = $info['month_pay'];
        $info['year_pay'] = ['month' => $info['year_pay'], 'year' => $info['year_pay'] * 12, 'deposit_money' => $info['deposit_money']];
        $info['season_pay'] = ['month' => $info['season_pay'], 'year' => $info['season_pay'] * 12, 'deposit_money' => $info['deposit_money']];
        $info['month_pay'] = ['month' => $info['month_pay'], 'year' => $info['month_pay'] * 12, 'deposit_money' => $info['deposit_money']];
        $a = explode('-',$info['room_number']);
        $info['building'] = $a[0];
        $info['floor'] = $a[1];
        $info['unit'] = $a[2];
        $info['room_number'] = $a[3];
        $this->ajaxReturn(['code' => 200, 'info' => $info]);
    }

     /**
      * 立即入住.
      */
     public function add_order()
     {
         $member_id = session('member_id');
         $id = I('param.id');     //房源id
         $skuid = I('param.skuid');  //房间id
         $building = I('param.building');  //栋
         $floor = I('param.floor');  //楼
         $unit = I('param.unit');  //单元
         $start_time = I('param.start_time');  //入住时间
         $payment_type = I('param.payment_type');  //1 表示月付  2表示季付  3表示年付
         if (empty($id) || !is_numeric($id) || empty($skuid) || !is_numeric($skuid)) {
             $this->ajaxReturn(['code' => 300, 'msg' => '编号错误']);
         }
         $model = new HousingModel();
         $hinfo = $model->where("id=$id")->find();
         $model = new OrderModel();
         // 生成订单号
         $d = getdate();
         $prefix = date('y');
         $prefix .= str_pad($d['yday'], 3, 0, STR_PAD_LEFT);
         $prefix .= str_pad(time() - strtotime(date('Y-m-d')), 5, 0, STR_PAD_LEFT);
         $order_id = $prefix.str_pad(substr($member_id, -5, 5), 5, 0, STR_PAD_LEFT).rand(0, 9);
         $data['building'] = $building;
         $data['floor'] = $floor;
         $data['unit'] = $unit;
         $data['start_time'] = $start_time;
         $data['payment_type'] = $payment_type;
         $data['order_id'] = $order_id;
         $data['room_number'] = $hinfo['room_number'];
         $data['housing_id'] = $id;
         $data['skuid'] = $skuid;
         $data['member_id'] = $member_id;
         $data['create_time'] = date('Y-m-d H:i:s', time());
         $res = $model->add($data);
         if ($res) {
             $this->ajaxReturn(['code' => 200, 'msg' => '提交成功']);
         } else {
             $this->ajaxReturn(['code' => 300, 'msg' => '提交失败']);
         }
     }
}
